﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using cqp.tian.bgtz.me.Code.Enum;

namespace cqp.tian.bgtz.me.Code.Common
{
    public class ReadXml
    {
        /// <summary>
        /// 获取更新的书籍信息
        /// </summary>
        /// <returns></returns>
        public static List<Book> GetBooksData()
        {
            var bookList = new List<Book>();
            var doc = new XmlDocument();
            doc.Load(CommonApi.MainPath + "BookGroup\\Book.xml");
            if (doc.DocumentElement == null) return bookList;
            var nodeList = doc.DocumentElement.FirstChild.ChildNodes;

            foreach (XmlElement node in nodeList)
            {
                var book = new Book
                {
                    Name = node.GetAttribute("name"),
                    Id = node.GetAttribute("id"),
                    Code = node.GetAttribute("code"),
                    TransferUrl = node.GetAttribute("transferUrl"),
                    CoverImageUrl = node.GetAttribute("coverImageUrl"),
                    Group = new List<Group>()
                };

                foreach (XmlElement childNode in node.ChildNodes)
                {
                    book.Group.Add(new Group() { 
                        GroupNo = childNode.InnerText,
                        IsAtAll= childNode.GetAttribute("isAtAll") == "Y" ? true : false ,
                        IsSendImage= childNode.GetAttribute("isSendImage") == "Y" ? true : false,
                        IsSendShare= childNode.GetAttribute("isSendShare") == "Y" ? true : false,
                        ImageName = childNode.GetAttribute("imageName")
                        
                    });
                }

                bookList.Add(book);
            }

            return bookList;
        }

        /// <summary>
        /// 获取群信息
        /// </summary>
        /// <returns></returns>
        public static List<Group> GetGroupsData()
        {
            var groupList = new List<Group>();

            var doc = new XmlDocument();
            doc.Load(CommonApi.MainPath + "BookGroup\\Group.xml");
            if (doc.DocumentElement == null) return groupList;
            var nodeList = doc.DocumentElement.FirstChild.ChildNodes;

            groupList.AddRange(from XmlElement node in nodeList select new Group { GroupNo = node.InnerText, Text = node.GetAttribute("text"), IsSendImage = node.GetAttribute("isSendImage") == "Y" ? true : false, ImageName = node.GetAttribute("imageName") });

            return groupList;
        }
    }
}
